// pages/insureReminder/insureReminder.js
const app = getApp();
const util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    insureEndDate: '', //保险最大结束日期
    insureStartDate: '2010-01-01',
    exampleTile: '什么是保险到期时间',
    exampleImgUrl: app.globalData.apiImgUrl + '/example/insure.jpg',
    exampleConfirmBtnText: '确定',
    isHiddenExample: true,
    switchChecked: true,

    VehicleLicenseID: 0,
    InsureExpireDate: '', //保险到期日期
    IsInsure: 0, //是否保险提醒
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    let d = new Date();
    d.setFullYear(d.getFullYear() + 1);

    self.setData({
      insureEndDate: util.formatDate(d, 'yyyy-MM-dd'),
      VehicleLicenseID: options.VehicleLicenseID,
      InsureExpireDate: options.InsureExpireDate || '请选择时间', //保险到期日期
      IsInsure: options.IsInsure, //是否保险提醒
      switchChecked: options.IsInsure>0
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  saveTap:function(){
    let self = this;
    if (self.data.InsureExpireDate == '请选择时间') {
      wx.showToast({
        title: '请选择保险到期时间',
        icon: "none"
      });
      return;
    }


    let url = app.globalData.apiRootUrl + '/Car/SetVehicleRemind';
    app.httpRequest({
      url: url,
      showLoading: true,
      method: "POST",
      data: {
        VehicleLicenseID: self.data.VehicleLicenseID,
        InsureExpireDate: self.data.InsureExpireDate, //发动机号    
        IsInsure: self.data.IsInsure,
        ReMindType: 3
      },
      success: function (res) {
        let rdata = res.data;
        //console.log(res);
        if (rdata.status == 0) {
          wx.navigateBack();
        } else {
          wx.showToast({
            title: rdata.message,
            icon: 'none'
          });
        }
      }
    });
  },
  //开关切换
  switchChange: function(e) {
    console.log('switch1 发生 change 事件，携带值为', e.detail.value);
    this.setData({
      switchChecked: e.detail.value,
      IsInsure: e.detail.value ? 1 : 0
    })
  },
  //保险日期
  insureDataTap: function(e) {
    let self = this;
    self.setData({
      InsureExpireDate: e.detail.value,
    });
  },
  //开通保险提醒
  openInsureTap: function() {
    let self = this;
  },
  //示例图片确定按钮
  exampleConfirmTap: function() {
    let self = this;
    self.setData({
      isHiddenExample: true
    })
  },
  //示例图片显示
  showExampleTap: function() {
    let self = this;
    self.setData({
      isHiddenExample: false
    })
  }
})